package com.qingke.easyjava.besuper2.command.impi;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.qingke.easyjava.besuper2.BeSuperApplication;
import com.qingke.easyjava.besuper2.PlayerDao;
import com.qingke.easyjava.besuper2.command.SystemCommand;
import com.qingke.easyjava.besuper2.pojo.Player;
import com.qingke.easyjava.utils.QingkeConsole;

public class SignupCommand extends SystemCommand {

	@Override
	public void execute() {
		PlayerDao pd = new PlayerDao();
		String username = QingkeConsole.askUserInputString("Please enter your username");
		while(pd.isUsernameExists(username)) {
			QingkeConsole.println("Username has been exist!");
			username = QingkeConsole.askUserInputString("Please enter your username");
		}
		
		String password = QingkeConsole.askUserInputString("Please enter your password");
		
		while(!validdatePassword(password.trim())) {
			password = QingkeConsole.askUserInputString("Please enter your password");
		}
		
		String nickname = QingkeConsole.askUserInputString("Please enter your nickname");
		Player player = pd.signup(username, password, nickname);
		BeSuperApplication.getInstance().setPlayerProfile(player);
	}

	private boolean validdatePassword(String password) {
		// length > 6
		if (password.trim().length() < 6) {
			QingkeConsole.println("Password need at least 6 characters!");
			return false;
		}

		// not all word
		Pattern pn = Pattern.compile("[0-9]");
		Matcher numMatcher = pn.matcher(password);
		if (!numMatcher.find()) {
			QingkeConsole.println("Password need at least one number!");
			return false;
		}

		// not all number
		Pattern wordPn = Pattern.compile("[A-Za-z]");
		Matcher wordMatcher = wordPn.matcher(password);
		if (!wordMatcher.find()) {
			QingkeConsole.println("Password need at least one character!");
			return false;
		}
		
		return true;
	}

}
